<template>
  <div>
    <el-card class="box-card">
      <h2>部门</h2>
      <el-checkbox-group v-model="pitchdepartmentList" @change="handleCheckedCitiesChange">
        <el-checkbox v-for="item in departmentList" :key="item.id" :label="item.name">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
    </el-card>
    <el-card class="box-card">
      <el-table :data="newlist[x]" border style="width: 100%">
        <el-table-column type="index" label="序号" />
        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="workNumber" label="工号" />
        <el-table-column prop="departmentName" label="部门" />
        <el-table-column prop="mobile" label="手机号" />

      </el-table>
      <el-pagination
        :page-size="10"
        layout="total, prev, pager, next"
        :total="total ||0"
        @current-change="handleSizeChange"
      />
    </el-card>
  </div>
</template>

<script>
import { getMockAttendances } from '@/api/attendances.js'
import { department } from '@/api/department.js'
export default {
  data() {
    return {
      allList: [],
      newlist: [],
      pitchdepartmentList: [],
      departmentList: [],
      total: 0,
      x: 0
    }
  },
  created() {
    this.getMockAttendances()
    this.getdepartmentList()
  },
  methods: {
    async getMockAttendances() {
      const res = await getMockAttendances()
      this.allList = [...res.rows]
      //   console.log(this.allList)
      this.total = res.total
      const List2 = [...res.rows]
      this.arr(List2)
    //   this.arr(res.rows)
    //   console.log(this.allList)
    },
    // 获取部门列表
    async getdepartmentList() {
      const { depts } = await department()
      this.departmentList = depts.slice(1)
    },
    arr(list) {
      if (list.length > 0) {
        this.newlist.push(list.splice(0, 10))
        this.arr(list)
      }
    },
    handleSizeChange(val) {
      this.x = val - 1
    },
    async handleCheckedCitiesChange(val) {
      let list3 = []
      if (val.length !== 0) {
        list3 = this.allList.filter((ele) => {
          return val.includes(ele.departmentName)
        })
      } else {
        list3 = [...this.allList]
      }
      this.x = 0
      this.total = list3.length
      this.newlist = []
      this.arr(list3)
    }
  }
}
</script>

<style>
.el-card{
    margin-top: 20px;
}
</style>
